/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2022-03-26 02:58:38
 * @LastEditors: Sam
 * @LastEditTime: 2022-04-20 11:46:28
 */
import axios from 'axios'
import { getToken } from '@/utils/auth'

export function export_excell(url,params={}) {
 axios({
    method: 'post',
    url: url,
    data: params,
    responseType: 'blob',
    headers: {
      'Authorization': 'Bearer ' + getToken()
    }
  }).then(res => {
        let blob = new Blob([res.data], { type: "application/vnd.ms-excel" }); // 将服务端返回的文件流（二进制）excel文件转化为blob
        var fileName = '数据导出.csv'; // 导出的文件名
        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
            // IE
            window.navigator.msSaveOrOpenBlob(blob, fileName);
        } else {
            let objectUrl = (window.URL || window.webkitURL).createObjectURL(
            blob
            );
            let downFile = document.createElement("a");
            downFile.style.display = "none";
            downFile.href = objectUrl;
            downFile.download = fileName; // 下载后文件名
            document.body.appendChild(downFile);
            downFile.click();
            document.body.removeChild(downFile); // 下载完成移除元素
            // window.location.href = objectUrl
            window.URL.revokeObjectURL(objectUrl); // 只要映射存在，Blob就不能进行垃圾回收，因此一旦不再需要引用，就必须小心撤销URL，释放掉blob对象。
        }
  })
}

